
package com.jy.framework.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jy.framework.annotation.DataFilter;
import com.jy.framework.sys.dao.SysDeptDao;
import com.jy.framework.sys.entity.SysAreaEntity;
import com.jy.framework.sys.entity.SysDeptEntity;
import com.jy.framework.sys.service.SysAreaService;
import com.jy.framework.sys.service.SysDeptService;
import com.jy.framework.sys.service.TreeService;
import com.jy.framework.utils.Constant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {


    @Autowired
    private TreeService treeService;
    @Autowired
    private SysAreaService sysAreaService;

    @Override
    public boolean save(SysDeptEntity entity) {
        super.save(entity);
        treeService.updateParentIds(this, entity);
        return true;
    }

    @Override
    public boolean updateById(SysDeptEntity entity) {
        treeService.updateParentIds(this, entity);
        return super.updateById(entity);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        for (Serializable id :
                idList) {
            treeService.delByParentId(this, id);
        }
        return true;
    }

    @Override
    public void fillParentIds() {
        treeService.fillParentIds(this);
    }

    @Override
    @DataFilter(subDept = true, user = false)
    public List<SysDeptEntity> queryList(Map<String, Object> params) {
        List<SysDeptEntity> deptList =
                this.list(new QueryWrapper<SysDeptEntity>()
                        .inSql(params.get(Constant.SQL_FILTER) != null, "id", (String) params.get(Constant.SQL_FILTER)));

        Map<Long, SysDeptEntity> deptEntityMap = deptList.stream().collect(Collectors.toMap(key -> key.getId(), value -> value));
        Map<Long, String> idNameMap = sysAreaService.list(new QueryWrapper<>()).stream().collect(Collectors.toMap(SysAreaEntity::getId, SysAreaEntity::getName));
        for (SysDeptEntity sysDeptEntity : deptList) {
            SysDeptEntity parentDeptEntity = deptEntityMap.get(sysDeptEntity.getParentId());
            if (parentDeptEntity != null) {
                sysDeptEntity.setParentName(parentDeptEntity.getName());
            }
            if (null !=sysDeptEntity.getAreaId()) {
                sysDeptEntity.setAreaName(idNameMap.get(sysDeptEntity.getAreaId()));
            }
        }
        return deptList;
    }

    @Override
    public List<Long> queryDetpIdList(Long parentId) {
        return baseMapper.queryDetpIdList(parentId);
    }

    @Override
    public List<Long> getSubDeptIdList(Long deptId) {

        QueryWrapper<SysDeptEntity> query = new QueryWrapper<>();
        query.like("parent_ids", "," + deptId + ",");
        List<SysDeptEntity> list = list(query);

        //部门及子部门ID列表
        List<Long> deptIdList = new ArrayList<>();
        list.forEach(id -> {
            deptIdList.add(id.getId());
        });

        //获取子部门ID
//        List<Long> subIdList = queryDetpIdList(deptId);
//        getDeptTreeList(subIdList, deptIdList);

        return CollectionUtil.isEmpty(deptIdList)?new ArrayList<>():deptIdList;
    }

    /**
     * 递归
     */
    private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList) {
        for (Long deptId : subIdList) {
            List<Long> list = queryDetpIdList(deptId);
            if (list.size() > 0) {
                getDeptTreeList(list, deptIdList);
            }

            deptIdList.add(deptId);
        }
    }
}
